package com.terren.webdig.setting.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.terren.webdig.basic.BasicControllerSupport;
import com.terren.webdig.common.util.CommonUtil;
import com.terren.webdig.common.util.JsonUtil;
import com.terren.webdig.setting.entity.Function;
import com.terren.webdig.setting.service.IFunctionService;
/***
 * `u_function` `u_function_parent_child` `u_role` `u_role_function`
 * @author admin
 *
 */
@Controller
@RequestMapping(value = "/function")
public class FunctionController extends BasicControllerSupport {

	@Resource(name = "functionService")
	private IFunctionService functionService;

	@RequestMapping(value = "/getFunctionQuery", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getFunctionQuery(HttpServletRequest request) {
		String result = "";
		Function function = (Function) CommonUtil.getParameterObject(Function.class, request);
		try {
			List<Function> list = functionService.getFunctionQuery(function);
			result = JsonUtil.pagingJson(list);
		} catch (Exception e) {
			log.error("FunctionController.getFunctionQuery error", e);
		}
		return result;
	}

	/**
	 * 查出父节点名称作为下拉选项
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getParentName", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getParentName(HttpServletRequest request) {
		String result = "";
		Function function = (Function) CommonUtil.getParameterObject(Function.class, request);
		try {
			List<Function> list = functionService.getParentName(function);
			result = JsonUtil.pagingJson(list);
		} catch (Exception e) {
			log.error("FunctionController.getParentName error", e);
		}
		return result;
	}

	/**
	 * 验证K值,功能名称
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getFunctionK", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getFunctionK(HttpServletRequest request,String commitType) {
		String result = JsonUtil.messageJson(true, "");
		Function function = (Function) CommonUtil.getParameterObject(Function.class, request);
		Function functionK = new Function();
		Function functionName = new Function();
		try {
			functionK.setK(function.getK());
			List<Function> listK = functionService.getFunctionK(functionK);
			functionName.setFunctionName(function.getFunctionName());
			List<Function> listName = functionService.getFunctionK(functionName);
			switch (commitType){
			case "1"://新增
				if (listK.size() > 0) {
					result = JsonUtil.messageJson(false, "k值重复");
					return result;
				}
				if (listName.size()>0) {
					result = JsonUtil.messageJson(false, "功能名称重复");
					return result;
				}
				break;
			case "2"://修改
				if (listK.size() > 0) {
					if(!listK.get(0).getFunctionId().equals(function.getFunctionId())){
						result = JsonUtil.messageJson(false, "k值重复");
						return result;
					}	
				}
				if (listName.size() > 0) {
					if(!listName.get(0).getFunctionId().equals(function.getFunctionId())){
						result = JsonUtil.messageJson(false, "功能名称重复");
					}
					return result;
				}
				break;
			}	
		} catch (Exception e) {
			result = JsonUtil.messageJson(false, "服务器错误");
			log.error("FunctionController.getFunctionK error", e);
		}
		return result;
	}

	/**
	 * 添加
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addFunction", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addFunction(HttpServletRequest request) {
		String result = "";
		Function function = (Function) CommonUtil.getParameterObject(Function.class, request);
		try {
			functionService.addFunction(function);
			result = JsonUtil.messageJson(true, "添加成功");
		} catch (Exception e) {
			result = JsonUtil.messageJson(false, "添加失败");
			log.error("FunctionController.addfunction error", e);
		}
		return result;
	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateFunction", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateFunction(HttpServletRequest request) {
		String result = "";
		Function function = (Function) CommonUtil.getParameterObject(Function.class, request);
		try {
			functionService.updateFunction(function);
			// String json = JsonUtil.pagingJson(deleteParent_Child);
			result = JsonUtil.messageJson(true, "修改成功");
		} catch (Exception e) {
			result = JsonUtil.messageJson(false, "修改失败");
			log.error("FunctionController.updatefunction error", e);
		}
		return result;
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteFunction", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String deleteFunction(HttpServletRequest request) {
		String result = "";
		Function function = (Function) CommonUtil.getParameterObject(Function.class, request);
		//下面两个删除功能的顺序不能改动
		try {
			// 删除s_function表中的数据通过functionId
			functionService.deletefunction(function);
			// 删除父节或是子节点+角色功能对应
			functionService.deleteChildParentId(function);
			result = JsonUtil.messageJson(true, "删除成功");
		} catch (Exception e) {
			result = JsonUtil.messageJson(false, "删除失败");
			log.error("FunctionController.deletefunction error", e);
		}
		return result;
	}

	/**
	 * 查询function父子节点
	 */
	@RequestMapping(value = "/getRoleFunctionList", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getRoleFunctionList(HttpServletRequest request) {
		String result = "";
		Function function = (Function) CommonUtil.getParameterObject(Function.class, request);
		try {
			List<Function> rolefunctionList = functionService.getParentFunctionList(function);// 获取父功能
			List<Map<String, List<Function>>> arr = new ArrayList<Map<String, List<Function>>>();
			Map<String, List<Function>> map = new HashMap<String, List<Function>>();
			for (Function f : rolefunctionList) {
				function.setParentId(f.getFunctionId());
				List<Function> functions = functionService.getChildFunctionList(function);// 获取子功能
				map.put(f.getFunctionName() + "&&&" + f.getFunctionId(), functions);
			}
			arr.add(map);
			result = JsonUtil.pagingJson(arr);
		} catch (Exception e) {
			log.error("FunctionController.getRoleFunctionList", e);
		}
		return result;
	}
}
